package com.zretc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zretc.entity.Blog;
import com.zretc.service.BlogService;
import com.zretc.util.PageData;

@Controller
public class BlogController {

	// 注入service
	@Autowired
	private BlogService blogService;

	/**
	 * ajax的响应方式
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("blog/{id}")
	@ResponseBody
	public Blog listBlog(@PathVariable("id") int id) {
		Blog blog = blogService.selectBlog(id);
		return blog;
	}

	/**
	 * 页面转发的方式
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("rblog/{id}")
	public String listBlogs(@PathVariable("id") int id, Model model) {
		Blog blog = blogService.selectBlog(id);
		model.addAttribute("blog", blog);
		return "listblog";
	}

	/**
	 * 此时返回的数据格式，是我们自己定义PageData; 和前端要有约定，双发要之前就要达成协议;json格式一致
	 * 
	 * @param page
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@GetMapping(value = "blogpage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Blog> listBlogByPage(@RequestParam(value="page",defaultValue="1") int page,@RequestParam(value="limit",defaultValue="10") int limit,@RequestParam(value="keywords",defaultValue="") String keywords) {
		return blogService.selectBlogByLike(page, limit, keywords);
	}

}
